package top.tagao.study02_Set;

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.time.Year;
import java.util.Collection;
import java.util.HashSet;

/**
 * @author fu-xiao-liu
 * @Date 2022/4/15 21:05
 */
public class study_03_HashSet {
    public static void main(String[] args) {

        HashSet hashSet = new HashSet();
        hashSet.add(new Employee("小明",14,new MyDate(1,1,1)));
        hashSet.add(new Employee("小明",15,new MyDate(1,1,1)));
        hashSet.add(new Employee("小明",16,new MyDate(1,1,1)));

        System.out.println(hashSet);

    }
}
class Employee{
    private String name;
    private int age;
    private  MyDate birthDate;
    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println(hashCode());
    }

    public Employee(String name, int age, MyDate birthDate) {
        this.name = name;
        this.age = age;
        this.birthDate = birthDate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Employee employee = (Employee) o;

        if (name != null ? !name.equals(employee.name) : employee.name != null) return false;
        return birthDate != null ? birthDate.equals(employee.birthDate) : employee.birthDate == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (birthDate != null ? birthDate.hashCode() : 0);
        return result;
    }
}

class MyDate{
    int year;
    int month;
    int date;

    public MyDate(int year, int month, int date) {
        this.year = year;
        this.month = month;
        this.date = date;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        MyDate myDate = (MyDate) o;

        if (year != myDate.year) return false;
        if (month != myDate.month) return false;
        return date == myDate.date;
    }

    @Override
    public int hashCode() {
        int result = year;
        result = 31 * result + month;
        result = 31 * result + date;
        return result;
    }
}